
# Copyright 2009 Mithun Chaubey 
# 
# Licensed under the Apache License, Version 2.0 (the "License"); you 
# may not use this file except in compliance with the License. You may 
# obtain a copy of the License at 
# 
#     http://www.apache.org/licenses/LICENSE-2.0
#     
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing  permissions and
# limitations under the License.

''' Classes and functions for Living History events '''


import wiki
from google.appengine.ext import db


class event(wiki.entry):
    ''' Wiki entry class for events'''
    None
    
class event_revision(wiki.revision):
    ''' Wiki revision class for events'''
    title = db.StringProperty()
    durationEvent = db.BooleanProperty()
    start = db.DateTimeProperty()
    latestStart = db.DateTimeProperty()
    end = db.DateTimeProperty()
    earliestEnd = db.DateTimeProperty()
    description = db.TextProperty()
    
    def put(self):
        '''
        Function to add event revision to datastore
        @precondition: Revision must include a title
        @precondition: Revision must have a start date
        @precondition: Revisions for duration events must have an end date
        '''
        if self.title == None:
            raise ValueError, 'Event name not defined'
        if self.title == "":
            raise ValueError, 'Event name not defined'
        if self.start == None:
            raise ValueError, 'Start date not defined'
        if self.durationEvent:
            if self.end == None:
                raise ValueError, 'End date not defined for Duration Event'
            
        super(event_revision, self).put()
        
    def copy(self):
        '''
        Returns an event revision which is a copy of this one
        @rtype: event_revision
        '''
        ev_rev = event_revision()
        ev_rev.title = self.title
        ev_rev.durationEvent = self.durationEvent
        ev_rev.start = self.start
        ev_rev.latestStart = self.latestStart
        ev_rev.end = self.end
        ev_rev.earliestEnd = self.earliestEnd
        ev_rev.description = self.description
        
        return ev_rev

    
def create_new_event(ev_rev, user):
    '''
    Create a new event and put it in the datastore
    
    @param ev_rev: The initial event revision
    @param user: The user who created the event
    @type ev_rev: event_revision
    @type user: lh_user
    '''
    # Create new event
    ev = event()
    ev.creator = user
    ev.put()
    
    ev_rev.creator = user
    ev_rev.entry = ev
    ev_rev.comment = 'Initial version'
    
    try:
        ev_rev.put()
    except:
        if ev.is_saved:
            ev.delete()
        raise
    
    return ev

def get_event_by_key_str(key_str):
    '''
    Get an event from the datastore by its key string
    @param key_str: The event's datastore key string
    @type key_str: String
    '''
    ev = db.get(db.Key(key_str))
    if not isinstance(ev, event):
        raise ValueError, 'Key does not refer to event: '+ key_str
    return ev

def get_event_revision_by_key_str(key_str):
    '''
    Get an event revision by its key string
    @param key_str: The event revision's datastore key string
    @type key_str: String
    '''
    key = db.Key(key_str)
    ev_rev = db.get(key)
    if not isinstance(ev_rev, event_revision):
        raise ValueError, 'Key does not refer to event revision: ' + key_str
    return ev_rev
